package com.matongxue.springboot;

import com.github.pagehelper.util.StringUtil;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;

/**
 * 一个用来测试swagger注解的控制器
 * 注意@ApiImplicitParam的使用会影响程序运行，如果使用不当可能造成控制器收不到消息
 *
 */
@Controller
@RequestMapping("/say")
@Api(tags = "swagger例子")
public class SayController {

   @ResponseBody
   @RequestMapping(value ="/getUserName",method = RequestMethod.GET)
   @ApiOperation(value="根据用户编号获取用户姓名", notes="test: 仅1和2有正确返回")
   @ApiImplicitParam(paramType="query", name = "userNumber", value = "用户编号", required = true, dataType = "Integer")
   public String getUserName(@RequestParam(required = false,defaultValue = "") Integer userNumber){
        if(userNumber == 1){
            return "张三丰";
        }
        else if(userNumber == 2){
            return "慕容复";
        }
        else{
            return "未知";
        }
    }

    @ResponseBody
    @RequestMapping(value="/updatePassword", method= RequestMethod.POST)
    @ApiOperation(value="修改用户密码", notes="根据用户id修改密码")
    @ApiImplicitParams({
        @ApiImplicitParam(paramType="query", name = "userId", value = "用户ID", required = true, dataType = "Integer"),
        @ApiImplicitParam(paramType="query", name = "password", value = "旧密码", required = true, dataType = "String"),
        @ApiImplicitParam(paramType="query", name = "newPassword", value = "新密码", required = true, dataType = "String")
    })
    public String updatePassword(Integer userId,  String password,  String newPassword){
      if(userId <= 0 || userId > 2){
          return "未知的用户";
      }
      if(StringUtil.isEmpty(password) || StringUtil.isEmpty(newPassword)){
          return "密码不能为空";
      }
      if(password.equals(newPassword)){
          return "新旧密码不能相同";
      }
      return "密码修改成功!";
    }


    @ResponseBody
    @ApiOperation(value="获取用户详细信息", notes="根据url中的id来获取用户详细信息")
    @ApiImplicitParam(paramType="path",name = "id", value = "用户ID", required = true, dataType = "Integer")
    @GetMapping(value="/test/{id}")
    public String getUser(@PathVariable Integer id) {
        return ""+id;
    }

    @ResponseBody
    @ApiOperation(value="获取用户详细信息及token", notes="根据url中的id来获取用户详细信息")
    @ApiImplicitParams({
        @ApiImplicitParam(paramType="path", name = "id", value = "用户ID", required = false, dataType = "Integer"),
        @ApiImplicitParam(paramType="header", name = "token", value = "token信息", required = true, dataType = "String"),
    })
    @GetMapping(value="/test-header/{id}")
    public String getUser(@PathVariable("id") Integer id, @RequestHeader(value="token") String userAgent) {
        return "id "+id+" token "+userAgent;
    }

    @ResponseBody
    @ApiOperation("json测试更改用户信息")
    @PostMapping("/updateUserInfo")
    public Object updateUserInfo(@RequestBody User user){
       return user;
    }
}
